import {
  createRouter,
  createWebHashHistory,
  createWebHistory,
} from "vue-router";
import type { RouteRecordRaw } from "vue-router";
const Layout = () => import("@/Layout/Layout.vue");

const modules: AppRouteRecordRaw[] = []; // 全部引入路由列表
const noModulesName: string[] = []; // 不准备引入的文件
const files = import.meta.glob("./modules/*.ts", { eager: true }) as any;
for (const key in files) {
  const filename = key.replace(/(\.\/modules\/|\.(js|ts))/g, "");
  if (!noModulesName.includes(filename)) {
    const file = files[key].default || files[key];
    modules.push(file);
  }
}
/**
 * 动态路由，如果有需求的话
 */
export const asyncRouterMap: AppRouteRecordRaw[] = [];

export const constantRouterMap: AppRouteRecordRaw[] = [
  {
    path: "/login",
    name: "Login",
    component: () => import("@/views/login/login.vue"),
    meta: {
      title: "登录",
      hidden: true,
    },
  },
  {
    path: "/",
    name: "Root",
    component: () => import("@/views/index/index.vue"),
    meta: {
      isLogin: false,
      hidden: true,
      noTagsView: true,
    },
  },

  ...modules,
];

const router = createRouter({
  history: createWebHistory('/room/'),
  strict: false,
  routes: constantRouterMap as RouteRecordRaw[],
  scrollBehavior: () => ({ left: 0, top: 0 }),
});

export const resetRouter = (): void => {
  const resetWhiteNameList = ["Redirect", "Login", "NoFind", "Root"];
  router.getRoutes().forEach((route) => {
    const { name } = route;
    if (name && !resetWhiteNameList.includes(name as string)) {
      router.hasRoute(name) && router.removeRoute(name);
    }
  });
};

export default router;
